﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EasyModbus;

namespace _8轴运动控制
{
    public class Mitsubishi
    {
        //创建连接对象
        public ModbusClient mc = new ModbusClient();
        private bool IsConn = false;

        //连接PLC
        public bool Conncet(string IPAdress, string Port)
        {
            Disconncet();
            try
            {
                mc.Connect(IPAdress, Convert.ToInt32(Port));
                IsConn = true;
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        //断开连接
        public bool Disconncet()
        {
            try
            {
                mc.Disconnect();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //写M1线圈
        public bool WriteSingle(string str, bool value)
        {
            if (IsConn==false)
            {
                return false;
            }
            try
            {
                int address = DecodM(str);
                mc.WriteSingleCoil(address, value);//写M1
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        //读单个M1线圈
        public bool ReadSingle(string str)
        {
            if (IsConn == false)
            {
                return false;
            }
            try
            {
                int address = DecodM(str);
                bool[] value = mc.ReadCoils(address, 1);
                if (value[0] == true)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
            


        }
        //写D0寄存器
        public bool WriteRegister(string str, int value)
        {
            if (IsConn == false)
            {
                return false;
            }
            try
            {
                int address = DecodM(str);
                mc.WriteSingleRegister(address, value);//写D0
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //读D0寄存器
        public int ReadRegister(string str)
        {
            if (IsConn == false)
            {
                return -1;
            }
            try
            {
                int address = DecodM(str);
                int[] value = mc.ReadHoldingRegisters(address, 1);//读D0
                return value[0];
            }
            catch (Exception)
            {
                return -1;
            }
            
        }

        //M地址解析
        private int DecodM(string address)
        {
            string str = address.Substring(1);
            int value = Convert.ToInt32(str) + 8192;
            return value;
        }
        //D地址解析
        private int DecodD(string address)
        {
            string str = address.Substring(1);
            int value = Convert.ToInt32(str);
            return value;
        }

        //ASKII码解析 数字转字母
        public string DecodAskDA(int num)
        {
            string str = ((char)num).ToString();
            if (str=="Y")
            {
                str = "YH";
            }
            if (str == "W")
            {
                str = "WH";
            }
            return str;
        }

        //ASKII码解析 字母转数字
        public int DecodAskAD(char str)
        {
            int value = (int)str;
            return value;
        }


    }
}
